<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sa="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" method="POST" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">权限名称</label>
                    <div class="layui-input-inline">
                        <input id="permissionName" type="text" name="permissionName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="layui-btn layui-btn-md" lay-submit lay-filter="power-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="layui-btn layui-btn-md" id="resetBtn">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="power-table" lay-filter="power-table"></table>
    </div>
</div>
<script type="text/html" id="power-toolbar">
    <button class="layui-btn  layui-btn-md" lay-event="add" sa:hasPermission="permission_add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="layui-btn layui-btn-success layui-btn-md" lay-event="expandAll">
        <i class="layui-icon layui-icon-spread-left"></i>
        展开
    </button>
    <button class="layui-btn layui-btn-success layui-btn-md" lay-event="foldAll">
        <i class="layui-icon layui-icon-shrink-right"></i>
        折叠
    </button>
</script>

<script type="text/html" id="power-bar">
    <button class="layui-btn  layui-btn-sm" sa:hasPermission="permission_update" lay-event="edit"><i
            class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="layui-btn layui-btn-danger layui-btn-sm" sa:hasPermission="permission_delete" lay-event="remove"><i
            class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="power-enable">
    <input type="checkbox" name="status" value="{{d.permissionId}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="power-enable" {{ d.status== 0 ? 'checked' : '' }} />
</script>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:inline="javascript">
    layui.use(['table', 'form', 'jquery', 'treetable'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;


        window.render = function () {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'permissionId',
                treePidName: 'permissionParentId',
                skin: 'line',
                treeDefaultClose: true,
                toolbar: '#power-toolbar',
                elem: '#power-table',
                url: baseURL + 'permission/list',
                parseData: function (res) {
                    return res;
                },
                response: {
                    statusCode: 1000 //规定成功的状态码，默认：0
                },
                type: 'get',
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'permissionName', minWidth: 200, title: '权限名称'},
                        {field: 'permissionCode', title: '权限代码'},
                        {field: 'description', title: '权限描述'},
                        // {field: 'status', title: '是否可用', templet: '#power-enable'},
                        {title: '操作', templet: '#power-bar', width: 150, align: 'center'}
                    ]
                ]
            });
        }
        render();
        table.on('tool(power-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        })
        form.on('submit(power-query)', function (data) {
            treetable.render({
                treeColIndex: 1,
                treeSpid: 0,
                treeIdName: 'permissionId',
                treePidName: 'permissionParentId',
                skin: 'line',
                treeDefaultClose: true,
                toolbar: '#power-toolbar',
                elem: '#power-table',
                url: baseURL + 'permission/getParent',
                where: {
                    permissionName: data.field.permissionName
                },
                parseData: function (res) {
                    return res;
                },
                response: {
                    statusCode: 1000 //规定成功的状态码，默认：0
                },
                type: 'get',
                page: false,
                cols: [
                    [
                        {type: 'checkbox'},
                        {field: 'permissionName', minWidth: 200, title: '权限名称'},
                        {field: 'permissionCode', title: '权限代码'},
                        {field: 'description', title: '权限描述'},
                        {field: 'status', title: '是否可用', templet: '#power-enable'},
                        {title: '操作', templet: '#power-bar', width: 150, align: 'center'}
                    ]
                ]
            });
            return false;
        });
        form.on('switch(power-enable)', function (res) {
            $.ajax({
                url: baseURL + 'permission/enable/' + res.value,
                type: 'post'
            })
        })
        document.getElementById('resetBtn').addEventListener('click', function () {
            // 重新加载表格，不带任何过滤条件
            render({
                where: {}
            })
        });
        table.on('toolbar(power-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'expandAll') {
                treetable.expandAll("#power-table");
            } else if (obj.event === 'foldAll') {
                treetable.foldAll("#power-table");
            }
        });

        window.add = function () {
            layer.open({
                type: 2,
                title: '权限新增',
                shade: 0.1,
                area: ['800px', '670px'],
                content: 'power-add'
            });
        }
        let flag = false;
        window.edit = function (obj) {
            $.ajax({
                url: baseURL + 'permission/getRoleIdsByPermissionId',
                data: {
                    permissionId: obj.data.permissionId
                },
                type: 'get',
                success: function (res) {
                    if (res.data.length) {
                        flag = true

                    }
                }
            })
            window.permissionId = obj.data.permissionId
            $.ajax({
                url: baseURL + "permission/getPermissionByPermissionId/" + obj.data.permissionId,
                type: 'get',
                success: function (res) {
                    console.log(res)
                    window.permissionParentId = res.data.permissionParentId
                }
            })
            layer.open({
                type: 2,
                title: '权限修改',
                shade: 0.1,
                area: ['600px', '600px'],
                content: "power-edit",
                success: function (layero, index) {
                    let body = layer.getChildFrame('body', index)
                    let data = obj.data;
                    body.find('input[name=permissionName]').val(data.permissionName)
                    body.find('input[name=permissionCode]').val(data.permissionCode)
                    body.find('input[name=permissionCode]').prop('disabled', flag)
                    body.find('input[name=description]').val(data.description)
                }
            });
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该权限', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: baseURL + 'permission/getRoleIdsByPermissionId',
                    data: {
                        permissionId: obj.data.permissionId
                    },
                    type: 'get',
                    success: function (res) {
                        if (res.data != null) {
                            layer.msg('该权限或子权限被分配，请解除关系再删除', {
                                icon: 2,
                                time: 2000
                            });
                            layer.close(loading);
                        } else {
                            $.ajax({
                                url: baseURL + 'permission/delete/' + obj.data['permissionId'],
                                dataType: 'json',
                                type: 'post',
                                success: function (result) {
                                    if (result) {
                                        layer.close(loading);
                                        render()
                                    }
                                    layer.msg(result.msg)
                                }
                            })
                        }
                    }
                })
            });
        }
    })
</script>
</body>
</html>